﻿using MediatR;
using SlaughterItem.Common.Domains.Manages;
using SlaughterItem.Common.Domains.Setup;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.SetupCommand
{
    /// <summary>
    ///待宰圈设置显示
    /// </summary>
    public class WaitingQueryHandler : IRequestHandler<WaitingQueryCommand, Result>
    {
        /// <summary>
        /// 待宰圈设置仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Waitings> waitingRepository;
        /// <summary>
        /// 部门仓储
        /// </summary>
        private readonly IBaseRepositoryGet<Sections> sectionsbaseRepository;


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="waitingRepository">待宰圈设置仓储</param>
        /// <param name="sectionsbaseRepository">部门仓储</param>
        public WaitingQueryHandler(IBaseRepositoryGet<Waitings> waitingRepository, IBaseRepositoryGet<Sections> sectionsbaseRepository)
        {
            this.waitingRepository = waitingRepository;
            this.sectionsbaseRepository = sectionsbaseRepository;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(WaitingQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Success };
          
            try
            {
               var list = (from a in waitingRepository.GetAll()
                            join b in sectionsbaseRepository.GetAll()
                            on a.SectionId equals b.SectionId
                            where a.IsDel == 0
                            select new 
                            {
                               b.SectionName  ,
                              a.WaitingName    ,
                              a.WaitingId      ,
                              a.WaitingDuty    ,
                              a.WaitingNo      ,
                              a.WaitingRemark  ,
                              a.WaitingStatu   ,
                              a.WaitingType    ,
                              b. SectionId      ,
                            }).ToList();

              

                if (!string.IsNullOrEmpty(request.WaitingDuty))
                {
                    list = list.Where(x => x.WaitingDuty.Contains(request.WaitingDuty)).ToList();
                }

                if (!string.IsNullOrEmpty(request.WaitingType))
                {
                    list = list.Where(x => x.WaitingType == request.WaitingType).ToList();
                }

                var totalCount = list.Count;
                var totalPage = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize);
                list = list.OrderBy(x => x.WaitingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                var WaitingsData = new
                {
                    totalCount,
                    totalPage,
                    list
                };
                result.Data = WaitingsData;
            }
            catch (Exception)
            {

                throw;
            }

            return result;
        }
    }
}
